home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_04 / 9n04077a < prev    next >
Text File  |  1991-02-17  |  1KB  |  51 lines

  1.  
  2. /********************************************************************/
  3. /*    Check class allocation errors.  Copyright by Joe Schell 1989.     */
  4. /********************************************************************/
  5.  
  6. #ifndef CLASS_check_heap
  7. #define CLASS_check_heap
  8.  
  9. #include <iostream.h>
  10. #include <stddef.h>        // Used for ptrdiff_t definition.
  11.  
  12. // CHECK_HEAP_diff_:        Used get around segmented memory on IBMs.
  13. #if defined(__TURBOC__)  \
  14.         && (defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__))  
  15.     #define CHECK_HEAP_diff_        char huge *
  16. #else
  17.     #define CHECK_HEAP_diff_        char*
  18. #endif
  19.  
  20.  
  21. class check_heap
  22.     {
  23. public:
  24.     void start()    {  begin = new char;    delete begin; }
  25.     check_heap()    { start(); }
  26.  
  27.     void test(const char *s=0)    // Do a test. 
  28.         {
  29.         end = new char;
  30.         if (begin != end)
  31.             cerr << s
  32.                  << "Heap error: entry/exit difference = "
  33.                  << diff() << ".\n";
  34.         delete end;
  35.         }
  36.  
  37.     void testnew(const char *s=0)    // Do a test and reset.
  38.         { test(s); start(); }
  39.  
  40. private:
  41.     char *begin, *end;        // Beginning and end of allocation.
  42.  
  43.     ptrdiff_t diff()    const
  44.         { return (ptrdiff_t)
  45.                 ((CHECK_HEAP_diff_)end - (CHECK_HEAP_diff_)begin);}
  46.  
  47.     };    // End of check_heap class.
  48.  
  49. #endif
  50.  
  51.